package rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.dao.KorisnikDao;
import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.entitet.Korisnik;

@SuppressWarnings("serial")
public class PromenaLozinkeKontroler extends HttpServlet {

	@EJB
	private KorisnikDao cKorisnikDao;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		getServletContext().getRequestDispatcher("/jsp/promeniLozinku.jsp")
				.forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Korisnik korisnik = (Korisnik) req.getSession()
				.getAttribute("korisnik");
		String staraLozinka = req.getParameter("staraLozinka");
		String novaLozinka1 = req.getParameter("novaLozinka1");
		String novaLozinka2 = req.getParameter("novaLozinka2");

		if (korisnik.getLozinka().equals(staraLozinka)) {
			if (novaLozinka1.equals(novaLozinka2)) {
				korisnik.setLozinka(novaLozinka1);
				cKorisnikDao.stopi(korisnik);
				cKorisnikDao.posalji();
				getServletContext().getRequestDispatcher("/OdjavaKontroler")
						.forward(req, resp);
			}
		}
		getServletContext().getRequestDispatcher("/jsp/promeniLozinku.jsp")
				.forward(req, resp);
	}
}
